﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Book
{
    public partial class Form4_5 : Form
    {
        public Form4_5()
        {
            InitializeComponent();
        }

        private void Form4_5_Load(object sender, EventArgs e)
        {
            label10.Text = string.Format("读者编号：{0}", UserHelper.UserName);
            timer1.Start();
        }

        private void 查询图书_Click(object sender, EventArgs e)
        {
            OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\zAllstation\access\bookmanage.accdb");
            string sql = "select * from book where 书名='" + textBox1.Text+ "'";
            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, thisConnection);
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            thisAdapter.Fill(thisDataSet, "table");
            DataTable dt = thisDataSet.Tables["table"];
            this.dataGridView1.DataSource = dt;
            thisConnection.Close();
        }

        private void 借阅图书_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            string now = dt.ToString();
            string plan = dt.AddDays(60).ToString();
            OleDbConnection DBconnect1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\zAllstation\access\bookmanage.accdb");
            DBconnect1.Open();
            string sqlcmd1 = string.Format("update book set 是否借阅='已借阅' where 书号 ='{0}'", textBox2.Text);
            OleDbCommand oleDbCommand1 = new OleDbCommand(sqlcmd1, DBconnect1);
            int i1 = oleDbCommand1.ExecuteNonQuery();
            if (textBox2.Text != null)
            {

                DialogResult result = MessageBox.Show("确定借阅此书吗？", "提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    OleDbConnection DBconnect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\zAllstation\access\bookmanage.accdb");
                    DBconnect.Open();
                    string sqlcmd = string.Format("insert into [isRead](读者编号,书号,借阅日期,应还日期) values('{0}','{1}','{2}','{3}') ", UserHelper.UserName, textBox2.Text, now, plan);

                    OleDbCommand oleDbCommand = new OleDbCommand(sqlcmd, DBconnect);
                    int i = oleDbCommand.ExecuteNonQuery();
                    MessageBox.Show("借阅成功！!");

                }
                if (result == DialogResult.Cancel)
                {
                }
            }
            else
            {
                MessageBox.Show("请输入书名！");
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
            this.Close();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4_4 form4_4 = new Form4_4();
            form4_4.Show();
        }

        private void 查看个人信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4_1 form4_1 = new Form4_1();
            form4_1.Show();
        }

        private void 查看个人借阅ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4_2 form4_2 = new Form4_2();
            form4_2.Show();
        }

        private void 修改个人信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4_3 form4_3 = new Form4_3();
            form4_3.Show();
        }

        private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定注销此用户吗？", "提示", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                OleDbConnection DBconnect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\zAllstation\access\bookmanage.accdb");
                DBconnect.Open();
                string sql = string.Format("delete from reader where 读者编号='{0}'", UserHelper.UserName);             //删除执行             
                OleDbCommand oleDbCommand = new OleDbCommand(sql, DBconnect);
                int i = oleDbCommand.ExecuteNonQuery();
                MessageBox.Show("注销成功！");
                Form1 form1 = new Form1();
                form1.Show();
                this.Close();
            }
            else
            {
                Form3 form3 = new Form3();
                form3.Show();
                this.Close();
            }
        }

        private void 查看图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4_5 form4_5 = new Form4_5();
            form4_5.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label3.Text = DateTime.Now.ToString(); 
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void 查看全部书籍_Click(object sender, EventArgs e)
        {
            OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\zAllstation\access\bookmanage.accdb");
            string sql = "select * from book";
            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, thisConnection);
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            thisAdapter.Fill(thisDataSet, "table");
            DataTable dt = thisDataSet.Tables["table"];
            this.dataGridView1.DataSource = dt;
            thisConnection.Close();
        }
    }
}
